package com.ndd.controller;

import com.ndd.constant.Constants;
import com.ndd.dto.AddressDTO;
import com.ndd.result.R;
import com.ndd.service.AddressService;
import com.ndd.utils.JwtUtils;
import com.ndd.vo.AddressVO;
import com.ndd.vo.UserLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 2024/4/27 16:07
 * wuhon
 * 地址controller
 */
@RestController
@RequestMapping("/api/address")
public class AddressController {
    @Autowired
    private AddressService addressService;

    /**
     * 查询用户地址信息
     * @param httpServletRequest
     * @return
     */
    @GetMapping("list")
    public R<List<AddressVO>> getAddressList(HttpServletRequest httpServletRequest){
        //获得请求头中的token数据
        String token = httpServletRequest.getHeader(Constants.TOKEN_HEADER);
        //解析令牌获得当前用户
        UserLoginVO loginVO = JwtUtils.parseToken(token);
        List<AddressVO> addressVOList = addressService.getAddressListByUserId(loginVO);
        return R.ok(addressVOList);
    }

    /**
     * 添加地址信息
     * @param addressDTO
     * @param httpServletRequest
     * @return
     */
    @RequestMapping("save")
    public R<Void> save(@RequestBody AddressDTO addressDTO, HttpServletRequest httpServletRequest){
        //获得请求头中的token数据
        String token = httpServletRequest.getHeader(Constants.TOKEN_HEADER);
        //解析令牌获得当前用户
        UserLoginVO loginVO = JwtUtils.parseToken(token);
        addressService.save(addressDTO,loginVO);
        return R.ok();
    }
}
